<?php

require_once '../../includes/includes.php';

if (isset($_POST['album'])) {

    $allbumName = $_POST['album'];
    $alt = $_POST['alt'];

    if (trim($allbumName) == '' || trim($alt) == '') {
        echo '<p>Empty fields !</p>';
        return false;
    }

    $table = 'albums';
    $fields = '*';
    $where_sql = 'name = :name';
    $where_data = array('name' => $allbumName);

    $db->select($table, $fields, $where_sql, $where_data);
    $db->fetchAll();

    $dirPath = '../../albums/' . $allbumName;

    if ($db->rowcount() > 0 || is_dir($dirPath)) {
        echo 'Album with this name has already created !';
        return;
    }

    mkdir($dirPath);

    $table = 'albums';
    $data = array(
        'name' => $allbumName,
        'alt' => $alt
    );

    $db->insert($table, $data);

    return true;
}